package pe.tnova.generic.structure.project.springmvc.soruce;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.dialogs.MessageDialog;

import pe.tnova.generic.structure.project.springmvc.generic.DAOBaseGeneric;
import pe.tnova.generic.structure.project.springmvc.util.ConstantesTNOVA;


public class SourceProjectCore {
	private IJavaProject javaProject = null;
	private IFolder srcJAVA = null;
	private String strPackageBase = null;
	private ICompilationUnit[] compilationUnit = null;
	private List<ICompilationUnit> listaICompilationUnits = null;
	
	private SourceProjectCore(IProject project, IJavaProject javaProject, IFolder srcJAVA) {
		this.javaProject = javaProject;
		this.srcJAVA = srcJAVA;		
	}

	public static SourceProjectCore getInstance(IProject project, IJavaProject javaProject, IFolder srcJAVA) {
		return new SourceProjectCore(project,javaProject,srcJAVA);
	}

	public void packageBase(String strPackageBase) {
		this.strPackageBase = strPackageBase;
	}

	public boolean existe(String strPackageDominio) throws JavaModelException {
		return existePackage(strPackageBase+ConstantesTNOVA.DONT+strPackageDominio);
	}

	public boolean existePackage(String strpackage) throws JavaModelException{
		IPackageFragment pack = getPackage(strpackage, false);
		return pack.exists();
	}
	
	public IPackageFragment getPackage(String strpackage, boolean create) throws JavaModelException{
		System.out.println("getPackage :"+strpackage + " create:"+create);
		IPackageFragmentRoot srcFolder = javaProject.getPackageFragmentRoot(srcJAVA);
		IPackageFragment pack = srcFolder.getPackageFragment(strpackage);	
		return pack;
	}
	
	public IPackageFragment createPackate(String strpackage, boolean create) throws JavaModelException{
		System.out.println("createPackate :"+strpackage + " create:"+create);
		IPackageFragmentRoot srcFolder = javaProject.getPackageFragmentRoot(srcJAVA);
		IPackageFragment pack = getPackage(strpackage, create);
		if(!pack.exists()){
			if(create){
				pack = srcFolder.createPackageFragment(strpackage, true, null);	
				return pack;
			}			
		}else{
			return pack;
		}
		return null;
	}
	
	public void standarDominio() throws JavaModelException {
		String strpackDominio = strPackageBase+ConstantesTNOVA.DONT+ConstantesTNOVA.PACK_DOMINIO;
		IPackageFragment packDominio = getPackage(strpackDominio, false);		
		compilationUnit =  packDominio.getCompilationUnits();
		IType[] iTypes = null;
		System.out.println("count compilationUnit:"+compilationUnit.length);
		if(compilationUnit.length>0){
			listaICompilationUnits = new ArrayList<ICompilationUnit>();
			for (ICompilationUnit iCompilationUnit : compilationUnit) {
				iTypes = iCompilationUnit.getTypes();
				for (IType iType : iTypes) {
					ITypeHierarchy typeHier= iType.newSupertypeHierarchy(null);
					IType[] superInterfaces= typeHier.getAllInterfaces();
					for (IType iType2 : superInterfaces) {
						if(iType2.getElementName().equals(ConstantesTNOVA.CLASS_ENTIDAD)){
							listaICompilationUnits.add(iCompilationUnit);
						}
					}				
				}				
			}
		}else{
			MessageDialog.openInformation(null,ConstantesTNOVA.RECURSOS_CORE_DAO,"No hay clases que extendan de la interfaz "+ConstantesTNOVA.CLASS_ENTIDAD);			
			return;
		}
	}

	
	public void generarDAOBase() throws JavaModelException {	
		IPackageFragment packDAOBase = null;
		if(!existePackage(strPackageBase + ConstantesTNOVA.DONT+ ConstantesTNOVA.PACK_DAO_BASE)){
			packDAOBase = createPackate(strPackageBase + ConstantesTNOVA.DONT+ ConstantesTNOVA.PACK_DAO_BASE, true);
		}else{
			packDAOBase = getPackage(strPackageBase+ConstantesTNOVA.DONT+ConstantesTNOVA.PACK_DAO_BASE, false);
		}		
		DAOBaseGeneric daoBaseGeneric = new DAOBaseGeneric(this,strPackageBase,packDAOBase,compilationUnit);
		daoBaseGeneric.generar();	
	}

//	public void generarDAO()  throws JavaModelException {		
//		
//	}
//
//	public void generarDAOImpl() throws JavaModelException  {		
//		
//	}
//
//	public void generarException() throws JavaModelException  {
//				
//	}


	public List<ICompilationUnit> getListaICompilationUnits() {
		return listaICompilationUnits;
	}

	public void setListaICompilationUnits(List<ICompilationUnit> listaICompilationUnits) {
		this.listaICompilationUnits = listaICompilationUnits;
	}
	
	
}
